#include "PowerUp.h"

#define JUMP_VELOCITY_BOOST -8.0f
#define FALLDOWN_VELOCITY_DECREASE -0.5f
#define MOVING_SPEED 2.0f

CPowerUp::CPowerUp(void)
{
}


CPowerUp::~CPowerUp(void)
{
}

void CPowerUp::LoadResource(LPDIRECT3DDEVICE9 d3ddv, LPD3DXSPRITE &SpriteHandler, HINSTANCE hInstance, HWND hWnd)
{
	_Sprite = CSpriteManager::getInstance()->GetObjectSprite(Object::PowerUp);
}

void CPowerUp::Spawn(D3DXVECTOR2 position, int type)
{
	SetPosition(position.x, position.y);
	_type = type;
	_vectorY = JUMP_VELOCITY_BOOST;
	_vectorX = MOVING_SPEED;
}

void CPowerUp::Update()
{
	if (_y > GROUNDY) 
	{
		_vectorY -= FALLDOWN_VELOCITY_DECREASE;
	}
	else 
	{
		_y = GROUNDY;
		_vectorY = 0;
		_vectorX = 0;
	}
	_x += _vectorX;
	_y -= _vectorY;
}

void CPowerUp::Render(D3DXVECTOR2 ContraPosition)
{
	if(abs(ContraPosition.x - _x) < SCREEN_WIDTH/2 + 64)
	{
		Update();
		int vpx = ContraPosition.x - SCREEN_WIDTH/2;
		if (vpx<=0) 
			vpx=0;
		if (vpx >= GAMEWORLDWIDTH - SCREEN_WIDTH)
		{
			vpx = GAMEWORLDWIDTH - SCREEN_WIDTH;
		}

		_Sprite->Render(_x, _y, _type);
	}
}